<template>
  <div class="box5">
    <el-container>
      <el-header>
        <p>
          <el-select v-model="order" placeholder="请选择" size="medium">
            <el-option value="升序">升序</el-option>
            <el-option value="降序">降序</el-option>
          </el-select>
          <el-input
            placeholder="请输入内容"
            size="medium"
            v-model="keyword"
          ></el-input>
          <el-button type="success" size="medium" @click="sou">搜索</el-button>
        </p>
        <p>
           <el-button type="success" size="medium" @click="shanall">选中删除</el-button>
          <el-button type="success" size="medium" @click="cj"
            >创建相册</el-button
          >
          <el-button type="success" size="medium" @click="flag4=true">上传图片</el-button>
        </p>
      </el-header>
      <el-container class="box6">
        <el-aside width="200px">
          <!-- 左侧列表 -->
          <ul class="box7">
            <li v-for="(item, key) in leftlist" :key="key">
              <span @click="dj(item.id)">{{ item.name }}</span>
              <el-dropdown>
                <span class="el-dropdown-link">
                  {{ item.images_count
                  }}<i class="el-icon-arrow-down el-icon--right"></i>
                </span>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item @click.native="xg(item.id)"
                    >修改</el-dropdown-item
                  >
                  <el-dropdown-item @click.native="del(item.id)"
                    >删除</el-dropdown-item
                  >
                </el-dropdown-menu>
              </el-dropdown>
            </li>
          </ul>
        </el-aside>
        <el-main>
          <ul class="box8">
            <li v-for="(item, key) in imglist" :key="key">
              <el-image :src="item.url"  :preview-src-list="[item.url]"></el-image>
              <p>{{ item.name }}</p>
              <p>
                <input type="checkbox" v-model='ids' :value='item.id'>
                <el-button
                  icon="el-icon-edit"
                  size="mini"
                  @click="gaitu(item.name, item.id)"
                >
                </el-button>
                <el-button
                  icon="el-icon-delete"
                  size="mini"
                  @click="deltu(item.id)"
                ></el-button>
              </p>
            </li>
          </ul>
        </el-main>
      </el-container>
    </el-container>
    <!-- 创建相册 -->
    <div class="tan" v-show="flag">
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="相册名称">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-input-number
          v-model="form.order"
          :min="1"
          :max="10"
          label="描述文字"
        ></el-input-number>
        <el-button type="success" @click="tj">提交</el-button>
      </el-form>
    </div>
    <!-- 修改相册 -->
    <div class="tan" v-show="flag1">
      <el-form ref="form" :model="xglist" label-width="80px">
        <el-form-item label="相册名称">
          <el-input v-model="xglist.name"></el-input>
        </el-form-item>
        <el-input-number
          v-model="form.order"
          :min="1"
          :max="10"
          label="描述文字"
        ></el-input-number>
        <el-button type="success" @click="xgsj">提交</el-button>
      </el-form>
    </div>
    <!-- 修改图片名字 -->
    <el-dialog title="修改图片名字" :visible.sync="flag2">
      <el-form :model="tu">
        <el-form-item label="图片名称">
          <el-input v-model="tu.tuname" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="flag2 = false">取 消</el-button>
        <el-button type="primary" @click="over(shantu)">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 上传图片 -->
    <el-dialog title="上传图片" :visible.sync="flag4" class="sc">
      <el-upload
      class="upload-demo"
      drag
      action="/admin/image/upload"
      multiple
      :headers='{token:this.$store.state.token}'
      name='img'
      :on-success='success'
      :data="{'image_class_id':id}"
    >
      <i class="el-icon-upload"></i>
      <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
      <div class="el-upload__tip" slot="tip">
        只能上传jpg/png文件，且不超过500kb
      </div>
    </el-upload>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      //左侧数据
      leftlist: [],
      //点击获取id
      id: 1,
      //页码
      page: 1,
      //每页数量
      limit: 6,
      //升降序
      order: "",
      //搜索词
      keyword: "",
      //图片列表
      imglist: [],
      //创建相册
      form: {
        name: "",
        order: "",
      },
      //创建相册标识
      flag: false,
      //删除id
      deleteid: "",
      //修改标识
      flag1: false,

      xglist: {
        xgid: "",
        name: "",
        order: "",
      },
      //图片名字
      tu: {
        tuname: "",
      },
      //删除单张图片id
      shantu: "",
      //修改相册标识
      flag2: false,
      //选中删除
      ids: [],
      //上传图片
      flag4:false,
      //上传图片信息
      xinxi:{
        image_class_id:'',
        name:''
      }
    };
  },
  methods: {
    dj(id) {
      this.id = id;
      this.getimg();
    },
    //搜索
    sou() {
      this.getimg();
    },
    //渲染页面
    getimg() {
      this.$http
        .get(
          `/admin/imageclass/${this.id}/image/${this.page}?limit=${this.limit}&order=${this.order}&keyword=${this.keyword}`
        )
        .then((res) => {
          // console.log(res);
          this.imglist = res.data.data.list;
          // console.log(this.imglist);
        });
    },
    //左侧列表
    getleft() {
      this.$http.get("/admin/imageclass/1").then((res) => {
        // console.log(res);
        this.leftlist = res.data.data.list;
        // console.log(this.leftlist);
        this.id = res.data.data.list[0].id;
        // console.log(this.id);
        this.getimg();
      });
    },
    //增加相册分类
    cj() {
      this.flag = true;
    },
    //添加相册
    tj() {
      this.$http
        .post("/admin/imageclass", this.form)
        .then((res) => {
          console.log(res);
          // this.getimg()
          this.getleft();
          this.flag = false;
        })
        .catch((err) => {
          console.log(err.response);
        });
    },
    //删除相册
    del(id) {
      this.deleteid = id;
      this.$http
        .delete(`admin/imageclass/${this.deleteid}`)
        .then((res) => {
          // console.log(res);
          this.getleft();
          this.$message.success("删除成功");
        })
        .catch((err) => {
          console.log(err.response);
        });
    },
    //修改
    xg(id) {
      this.flag1 = true;
      this.xglist.xgid = id;
    },
    //修改相册名字
    xgsj() {
      this.$http
        .post(`admin/imageclass/${this.xglist.xgid}?name=${this.xglist.name}`)
        .then((res) => {
          // console.log(res);
          this.$message.success("修改成功");
          this.flag1 = false;
          this.getleft();
        })
        .catch((err) => {
          console.log(err.response);
        });
    },
    //图片弹框
    gaitu(name, id) {
      this.tu.tuname = name;
      this.flag2 = true;
      this.shantu = id;
      // alert(name)
    },
    //修改图名
    over(id) {
      this.flag2 = false;
      this.shantu = id;
      this.$http
        .post(`/admin/image/${this.shantu}?name=${this.tu.tuname}`)
        .then((res) => {
          // console.log(res);
          this.$message.success("修改图片名称成功");
          this.getimg();
        });
    },
    //图片删除
    deltu(id) {
      this.shantu = id;
      this.$http.delete(`/admin/image/${this.shantu}`).then((res) => {
        console.log(res);
        this.$message.success("删除图片成功");
        this.getimg();
      });
    },
    //上传成功
    success(res){
     console.log(res)
     this.getimg()
    },
    //选中删除
    shanall(){
      console.log()
      this.$http.post('/admin/image/delete_all',{'ids':this.ids}).then((res)=>{
        console.log(res)
        this.$message.success('删除成功')
        this.getimg()
      }).catch(err=>{
        console.log(err.response)
      })
    }
  },
  mounted() {
    this.getleft();
  },
};
</script>

<style lang='scss' scoped>
.box5 {
  width: 100%;
  height: 100%;
  box-sizing: border-box;
  .el-container {
    width: 100%;
    height: 100%;
    .box6 {
      flex: 1;
    }
  }
}
.el-header {
  background-color: #ffffff;
  color: #333;
  text-align: center;
  line-height: 60px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
  border-bottom: 1px solid #dddddd;
  background: #cccccc;
  p {
    display: flex;
    justify-content: center;
    align-items: center;
  }
}

.el-aside {
  width: 220px !important;
  background-color: #ffffff;
  color: #333;
  text-align: center;
  // line-height: 60px;
  border-right: 1px solid #cccccc;
  .box7 {
    width: 100%;
    height: 100%;
    li {
      width: 100%;
      height: 60px;
      border: 1px solid #cccccc;
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 0 10px;
      // overflow: hidden;
      // text-overflow: ellipsis;
      // white-space: nowrap;
    }
    li:hover {
      color: rgb(110, 228, 110);
    }
  }
}

.el-main {
  background-color: #ffffff;
  color: #333;
  text-align: center;
  .box8 {
    width: 100%;
    height: 100%;
    display: flex;
    flex-wrap: wrap;
    list-style: none;
    // justify-content: space-between;
    li {
      width: 24%;
      margin-left: 1%;
      .el-image {
        width: 100%;
        height: 200px;
      }
      p {
        width: 100%;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }
    }
  }
  .tan {
    width: 400px;
    height: 300px;
    position: absolute;
    left: 200px;
    top: 300px;
    background: #e4d8d8;
  }
  .el-input {
    width: 88%;
  }
  .sc{
    text-align: center;
  }
}
</style>